/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyecto3;

import java.awt.Rectangle;
import java.net.URL;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *
 * @author Ulil Cojti, Maria Fernanda Martinez, Adrian Martinez
 */
public class inter extends javax.swing.JFrame {
    String path = "/proyecto3/Carro.png";  
    URL url = this.getClass().getResource(path); 
    String path1= "/proyecto3/b.png";
    URL url1 = this.getClass().getResource(path1);
    ImageIcon icon = new ImageIcon(url); 
    ImageIcon bandera=new ImageIcon(url1);
    
    int contador_banderas=0;
    
    int contador = 0;
    int ejeX, ejeY;
    JLabel carrito=new JLabel();
    String Bandera = "Flag";
    ArrayList<Bandera> Banderas = new ArrayList<Bandera>();
    String[] rutas = new String[10]; 
    String[] puntos =new String[10];
    InterfazGrafo grafo = new GraphMatrix();
    Floyd floyd;
    
    JLabel b1= new JLabel();
    JLabel b2= new JLabel();
    JLabel b3= new JLabel();
    JLabel b4= new JLabel();
    JLabel b5= new JLabel();
    JLabel b6= new JLabel();
    JLabel b7= new JLabel();
    JLabel b8= new JLabel();
    JLabel b9= new JLabel();
    JLabel b10= new JLabel();
  
    int id_ruta=0;
    Serial puerto= new Serial();
    Rectangle bounds;
    int x1,y1;
    /**
     *
     * @throws Exception
     */
    public inter() throws Exception {
        puerto.iniciar("COM1");
        for(int i=0;i<10;i++){
            rutas[i]="";
            puntos[i]="";
        }
        x1=5;
        y1=300;
         
        b1.setIcon(bandera);
        b2.setIcon(bandera);
        b3.setIcon(bandera);
        b4.setIcon(bandera);
        b5.setIcon(bandera);
        b6.setIcon(bandera);
        b7.setIcon(bandera);
        b8.setIcon(bandera);
        b9.setIcon(bandera);
        b10.setIcon(bandera);
        
        b1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b1MouseClicked(evt);
            }
        });
        
        b2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b2MouseClicked(evt);
            }
        });
        b3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b3MouseClicked(evt);
            }
        });
        b4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b4MouseClicked(evt);
            }
        });
        b5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b5MouseClicked(evt);
            }
        });
        b6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b6MouseClicked(evt);
            }
        });
        b7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b7MouseClicked(evt);
            }
        });
        b8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b8MouseClicked(evt);
            }
        });
        b9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b9MouseClicked(evt);
            }
        });
        b10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b10MouseClicked(evt);
            }
        });
        
        carrito.setIcon(icon);
        carrito.setBounds(x1, y1, 44, 44);
        bounds=carrito.getBounds();
        super.add(carrito);
        
        initComponents();
        
    }
    
    private void b1MouseClicked(java.awt.event.MouseEvent evt) {
        jLabel5.setText("Flag 1");
        jTextField2.setText("1");
    }
    
    private void b2MouseClicked(java.awt.event.MouseEvent evt) {
        jLabel5.setText("Flag 2");
        jTextField2.setText("2");
    }
    
    private void b3MouseClicked(java.awt.event.MouseEvent evt) {
        jLabel5.setText("Flag 3");
        jTextField2.setText("3");
    }
    
    private void b4MouseClicked(java.awt.event.MouseEvent evt) {
        jLabel5.setText("Flag 4");
        jTextField2.setText("4");
    }
    
    private void b5MouseClicked(java.awt.event.MouseEvent evt) {
        jLabel5.setText("Flag 5");
        jTextField2.setText("5");
    }
    
    private void b6MouseClicked(java.awt.event.MouseEvent evt) {
        jLabel5.setText("Flag 6");
        jTextField2.setText("6");
    }
    
    private void b7MouseClicked(java.awt.event.MouseEvent evt) {
        jLabel5.setText("Flag 7");
        jTextField2.setText("7");
    }
    
    private void b8MouseClicked(java.awt.event.MouseEvent evt) {
        jLabel5.setText("Flag 8");
        jTextField2.setText("8");
    }
    
    private void b9MouseClicked(java.awt.event.MouseEvent evt) {
        jLabel5.setText("Flag 9");
        jTextField2.setText("9");
    }
    
    private void b10MouseClicked(java.awt.event.MouseEvent evt) {
        jLabel5.setText("Flag 10");
        jTextField2.setText("10");
    }
   
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jButton7 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jButton1.setText("Up");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Left");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Right");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Down");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel1.setText("   ");
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });

        jTextField1.setEditable(false);
        jTextField1.setText("Click here to use the keyboard...");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jButton5.setText("Set reference point");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("View paths");
        jButton6.setEnabled(false);
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jTextField2.setText("0");
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jButton7.setText("Shortest path");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jLabel3.setText("Destiny");

        jTextField3.setText("0");

        jLabel4.setText("Start");

        jLabel2.setText("Start");

        jLabel5.setText("...");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 192, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGap(24, 24, 24)
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(15, 15, 15))))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton7)
                                .addGap(33, 33, 33))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(11, 11, 11)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(jButton2)
                                            .addGap(26, 26, 26)
                                            .addComponent(jButton3))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(jButton5)
                                            .addGap(11, 11, 11))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(jButton1)
                                            .addGap(44, 44, 44))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(jButton4)
                                            .addGap(42, 42, 42))))
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(75, 75, 75))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton6)
                                .addGap(46, 46, 46))))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextField2, jTextField3});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jButton3, jButton4});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton7)
                    .addComponent(jLabel2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
       
    }//GEN-LAST:event_formKeyPressed

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        int keyCode = evt.getKeyCode();
       
        switch(keyCode){
            case 37:
                jButton2.doClick();
                jLabel1.setText("Left");
                break;
            case 38:
                jLabel1.setText("Up");
                jButton1.doClick();
                break;
            case 39:
                jLabel1.setText("Right");
                jButton3.doClick();
                break;
            case 40:
                jLabel1.setText("Down");
                jButton4.doClick();
                break;
            case 32:
                jLabel1.setText("Set reference point");
                jButton5.doClick();
                break;
        }
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        jButton6.setEnabled(false);
        try {
            rutas[id_ruta] = rutas[id_ruta].concat(" UP");
            carrito.setBounds(x1,y1,bounds.height,bounds.width);
            puerto.send(1);
            Thread.sleep(200);
            puerto.send(0);
            y1--;
        } catch (Exception ex) {
            Logger.getLogger(inter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        jButton6.setEnabled(false);
        try {
            rutas[id_ruta] = rutas[id_ruta].concat(" LEFT");
            carrito.setBounds(x1,y1,bounds.height,bounds.width);
            puerto.send(3);
            Thread.sleep(200);
            puerto.send(0);
            x1--;
            y1--;
        } catch (Exception ex) {
            Logger.getLogger(inter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        jButton6.setEnabled(false);
        try {
            rutas[id_ruta] = rutas[id_ruta].concat(" RIGHT");
            carrito.setBounds(x1,y1,bounds.height,bounds.width);
            puerto.send(5);
            Thread.sleep(200);
            puerto.send(0);
            x1++;
            y1--;
        } catch (Exception ex) {
            Logger.getLogger(inter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        jButton6.setEnabled(false); 
        try {
            rutas[id_ruta] = rutas[id_ruta].concat(" DOWN");
            carrito.setBounds(x1,y1,bounds.height,bounds.width);
            puerto.send(8);
            Thread.sleep(200);
            puerto.send(0);
            y1++;
        } catch (Exception ex) {
            Logger.getLogger(inter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
        try {
            puerto.send(0);
            
        } catch (Exception ex) {
            Logger.getLogger(inter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jTextField1KeyReleased

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        jButton6.setEnabled(true);
        //indicador de punto de referencia
        
        if(contador_banderas<10){
            switch(contador_banderas){
                case 0: 
                    b1.setBounds(x1, y1, 44, 44);
                    super.add(b1);
                    jTextField3.setText("1");
                    break;
                case 1: 
                    b2.setBounds(x1, y1, 44, 44);
                    super.add(b2);
                    jTextField3.setText("2");
                    break;
                case 2: 
                    b3.setBounds(x1, y1, 44, 44);
                    super.add(b3);
                    jTextField3.setText("3");
                    break;
                case 3: 
                    b4.setBounds(x1, y1, 44, 44);
                    super.add(b4);
                    jTextField3.setText("4");
                    break;
                case 4: 
                    b5.setBounds(x1, y1, 44, 44);
                    super.add(b5);
                    jTextField3.setText("5");
                    break;
                case 5: 
                    b6.setBounds(x1, y1, 44, 44);
                    super.add(b6);
                    jTextField3.setText("6");
                    break;
                case 6: 
                    b7.setBounds(x1, y1, 44, 44);
                    super.add(b7);
                    jTextField3.setText("7");
                    break;
                case 7: 
                    b8.setBounds(x1, y1, 44, 44);
                    super.add(b8);
                    jTextField3.setText("8");
                    break;
                case 8: 
                    b9.setBounds(x1, y1, 44, 44);
                    super.add(b9);
                    jTextField3.setText("9");
                    break;
                case 9: 
                    b10.setBounds(x1, y1, 44, 44);
                    super.add(b10);
                    jTextField3.setText("10");
                    break;
            }
        }
        contador_banderas++;
        puntos[id_ruta]=" ";
        puntos[id_ruta]+=""+id_ruta+" "+(id_ruta+1);
        grafo.add(""+id_ruta);
       
        grafo.add(""+(id_ruta+1));
        if(id_ruta<10) {
            id_ruta++;
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        jButton6.setEnabled(false);
        carrito.setBounds(5,300,44,44); 
        jTextField3.setText("0");
        for(int i=0;i<10;i++){
            System.out.println(""+puntos[i]+" = "+rutas[i]);
            char[] arreglo = puntos[i].toCharArray();
            if(!rutas[i].equals("")){
                int peso=rutas[i].length();
                if(grafo.contains(""+arreglo[1])&&grafo.contains(""+arreglo[3])){
                    grafo.addEdge(""+arreglo[1], ""+arreglo[3], peso);
                }
            }  
        }
        
        Bandera bandera = new Bandera();             
        String CadN = String.valueOf(rutas[contador]);  //valueOf(String rutas[]);
        contador++;
        
        //Obtener los valores de las coordenadas X y Y
        ejeX = bandera.ObtenerPosicionX(bandera.DesConcatenarRIGHT(CadN), bandera.DesConcatenarLEFT(CadN));
        ejeY = bandera.ObtenerPosicionX(bandera.DesConcatenarUP(CadN), bandera.DesConcatenarDOWN(CadN));
        
        //Se instancia una nueva Bandera con los datos encontrados
        Bandera flag = new Bandera(ejeX, ejeY, Bandera.concat(String.valueOf(contador)) );
        System.out.println("Flags guardadas: " + flag.toString(flag)+"\n");
        grafo.show();
        floyd = new Floyd(grafo);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        jButton6.setEnabled(false);
        if(floyd!=null){
            floyd.caminoCorto();
            floyd.D.show();
            String punto1=jTextField3.getText();
            String punto2=jTextField2.getText();
            if(punto1!=null&&punto2!=null){
                if(grafo.contains(punto1)&&grafo.contains(punto2)){
                    int index1 = grafo.getIndex(punto1);
                    int index2 = grafo.getIndex(punto2);
                    System.out.print("Path to follow: "+punto1);
                    floyd.script += punto1;
                    floyd.mostrarIntermedias(index1, index2);
                    System.out.println(", "+punto2);
                    floyd.script += punto2;
                    Rectangle ref;
                    if(jTextField2.getText().equals("1")){
                        ref=b1.getBounds();
                        carrito.setBounds(ref.x, ref.y, 44, 44);
                    }
                    else if(jTextField2.getText().equals("2")){
                        ref=b2.getBounds();
                        carrito.setBounds(ref.x, ref.y, 44, 44);
                    }
                    else if(jTextField2.getText().equals("3")){
                        ref=b3.getBounds();
                        carrito.setBounds(ref.x, ref.y, 44, 44);
                    }
                    else if(jTextField2.getText().equals("4")){
                        ref=b4.getBounds();
                        carrito.setBounds(ref.x, ref.y, 44, 44);
                    }
                    else if(jTextField2.getText().equals("5")){
                        ref=b5.getBounds();
                        carrito.setBounds(ref.x, ref.y, 44, 44);
                    }
                    else if(jTextField2.getText().equals("6")){
                        ref=b6.getBounds();
                        carrito.setBounds(ref.x, ref.y, 44, 44);
                    }
                    else if(jTextField2.getText().equals("7")){
                        ref=b7.getBounds();
                        carrito.setBounds(ref.x, ref.y, 44, 44);
                    }
                    else if(jTextField2.getText().equals("8")){
                        ref=b8.getBounds();
                        carrito.setBounds(ref.x, ref.y, 44, 44);
                    }
                    else if(jTextField2.getText().equals("9")){
                        ref=b9.getBounds();
                        carrito.setBounds(ref.x, ref.y, 44, 44);
                    }
                    else if(jTextField2.getText().equals("10")){
                        ref=b10.getBounds();
                        carrito.setBounds(ref.x, ref.y, 44, 44);
                    }
                    jTextField3.setText(""+punto2);
                    int x;
                    for(int a=0;a<floyd.script.length()-1;a++){
                        x=a;
                        x++;
                        String limites = " "+floyd.script.charAt(a)+" "+floyd.script.charAt(x);
                        String tmp="";
                        for(int b=0;b<puntos.length;b++){
                            
                            if(puntos[b].equals(limites)){
                                System.out.println(rutas[b]);
                                tmp+=rutas[b];
                                }
                                
                            }
                        try {
                             String[] split = tmp.split(" ");
        int i=split.length;
        for(int j=0;j<i;j++){
            System.out.println("\n "+split[j]);
            if(split[j].equals("UP")){
                jButton1.doClick();
            }
            if(split[j].equals("DOWN")){
                jButton4.doClick();
            }
            if(split[j].equals("LEFT")){
                jButton2.doClick();
            }
            if(split[j].equals("RIGHT")){
                jButton3.doClick();
            }
             puerto.send(0);
        }
        
                        } catch (Exception ex) {
                            Logger.getLogger(inter.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    
                        
                        
                    }
                    floyd.script = "";
                }
            }              
        }
        else{
            System.out.println("No paths saved");
        }  
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jLabel1MouseClicked

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1KeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(inter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(inter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(inter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(inter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new inter().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(inter.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
